class  Calculator {
  expr = ''

  result = ''

  error = ''

  get display () {
    return this.result || this.expr
  }

  btns = [
    new ExprBtn('7', this), new ExprBtn('8', this), new ExprBtn('9', this), new ExprBtn('+', this),
    new ExprBtn('4', this), new ExprBtn('5', this), new ExprBtn('6', this), new ExprBtn('-', this),
    new ExprBtn('1', this), new ExprBtn('2', this), new ExprBtn('3', this), new ExprBtn('*', this),
    new ExprBtn('0', this), new ClearBtn('C', this), new EqOptBtn('=', this), new ExprBtn('/', this),
    new ExprBtn('.', this), new ExprBtn('(', this), new ExprBtn(')', this), new DelBtn('<', this)
  ]
}


class Btn {
  constructor (value, calculator) {
    this.value = value
    this.calculator = calculator
  }

  press () {
    this.calculator.error = ''
    this.calculator.result = ''
  }
}

class ExprBtn extends Btn {

  press () {
    super.press()
    this.calculator.expr = this.calculator.expr + this.value
  }
}

class EqOptBtn extends Btn {
  press () {
    super.press()
    try {
      if (!this.calculator.expr) return
      const r = eval(this.calculator.expr)
      this.calculator.result = String(r)
      this.calculator.expr = ''
      this.error = ''
    } catch (e) {
      this.calculator.error = '表达式错误，请检查!'
      console.error(e)
    }
  }
}

class ClearBtn extends Btn {
  press () {
    this.calculator.expr = ''
    this.calculator.result = ''
    this.calculator.error = ''
  }
}

class DelBtn extends Btn {
  press () {
    super.press()
    if (!this.calculator.expr) return
    this.calculator.expr = this.calculator.expr.slice(0, -1)
  }
}